﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NGourd.Core.Features;

namespace NGourd.Core.Parser
{
    public class FeatureParser
    {
        private readonly StreamReader source;
        private Feature feature;
        private IScenario currentScenario;

        public static Feature Parse(StreamReader source)
        {
            return new FeatureParser(source).Parse();
        }

        public static Feature Parse(FileInfo source)
        {
            return Parse(source.OpenText());
        }

        private FeatureParser(StreamReader source)
        {
            this.source = source;
        }

        private Feature Parse()
        {
            feature = new Feature();
            var line = source.ReadLine();
            while (line != null)
            {
                Parse(line.Trim());
                line = source.ReadLine();
            }
            return feature;
        }

        private void Parse(string line)
        {
            if (line.StartsWith("Feature: ", StringComparison.CurrentCultureIgnoreCase))
                feature.Name = line.Substring("feature: ".Length);
            else if (line.StartsWith("Scenario: ", StringComparison.CurrentCultureIgnoreCase))
                currentScenario = feature.Scenario(line.Substring("scenario: ".Length));
            else if (line.StartsWith("Given ", StringComparison.CurrentCultureIgnoreCase))
                currentScenario.Given(line.Substring("given ".Length));
            else if (line.StartsWith("When ", StringComparison.CurrentCultureIgnoreCase))
                currentScenario.When(line.Substring("when ".Length));
            else if (line.StartsWith("Then ", StringComparison.CurrentCultureIgnoreCase))
                currentScenario.Then(line.Substring("then ".Length));
            else if (line.StartsWith("And ", StringComparison.CurrentCultureIgnoreCase))
                currentScenario.And(line.Substring("and ".Length));
            else
            {
                //Console.WriteLine("UNKNOWN LINE: {0}", line);
            }
        }

    }
}
